package com.macaline.framemarker.db;

import com.macaline.technology.redis.util.RedisTemplate;
import com.macaline.util.ThreadUtil;

import java.util.concurrent.TimeUnit;

public class Main {


    ThreadDB db = new ThreadDB();
    public static void main(String[] args) {
//        try {
//            Main main = new Main();
//            main.init();
//            for (int i = 0; i < 100000; i++) {
//                for (int j = 1; j <= 5; j++) {
//                    int finalJ = j;
//                    new Thread(()->{
//                        try {
//                            System.out.println(main.get(finalJ + ""));
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }).start();
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            ThreadUtil.shutdown();
//            RedisTemplate.shutDown();
//        }
        try {
            Main main = new Main();
            main.init();
            for (int i = 0; i < 100000; i++) {
                for (int j = 1; j <= 5; j++) {
                    int finalJ = j;
                    ThreadUtil.execute(()->{
                        try {
                            TimeUnit.SECONDS.sleep(1);
                            main.get(finalJ + "");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ThreadUtil.shutdown();
//            RedisTemplate.shutDown();
        }

    }
    
    

    private Object get(String key) throws Exception {
        Object object = RedisTemplate.getObject(key);
        if(object != null){
            System.out.println("redis缓存中拿到数据");
            return object;
        }
        return db.select(key);
    }

    private void init() throws Exception {
        for (int i = 1; i <= 5; i++) {
            RedisTemplate.setObject(i + "", i + "", 2);
        }
    }
}
